package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis"
	"log"
	"oa_kadia_link/config"
	"oa_kadia_link/globe"
	"oa_kadia_link/utils"
)

func init() {
	globe.GetRdb().Set("[:serverStatus]", config.GetEnv().DefaultServerStatus, 0)
}

func ServerStatus() gin.HandlerFunc {
	return func(context *gin.Context) {
		if utils.IsAdmin(context) {
			return
		}
		rdb := globe.GetRdb()
		status, err := rdb.Get("[:serverStatus]").Int()
		if err == redis.Nil || status == 0 {
			utils.ErrorJSON(context, "此服务已关闭！")
			context.Abort()
			if err != nil {
				log.Println(err)
			}
			return
		}
	}
}
